import wechat from '@/script/wechat.js'
/**
 * 时间格式化
 * @param {String} time
 * @param {String} cFormat
 */
 export function parseTime(time, cFormat) {
	if (arguments.length === 0) {
	  return null
	}
  if (!time) return ''
  /* 修复IOS系统上面的时间不兼容*/
  if (time.toString().indexOf('-') > 0) {
    time = time.replace(/-/g, '/')
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    return value.toString().padStart(2, '0')
  })
  return time_str
}

/**
 * This is just a simple version of deep copy
 * Has a lot of edge cases bug
 * If you want to use a perfect deep copy, use lodash's _.cloneDeep
 * @param {Object} source
 * @returns {Object}
 */
export function deepClone(source) {
	if (!source && typeof source !== 'object') {
		throw new Error('error arguments', 'deepClone')
	}
	const targetObj = Object.prototype.toString.call(source) === "[object Array]" ? [] : {}
	Object.keys(source).forEach(keys => {
		if (source[keys] && typeof source[keys] === 'object') {
			targetObj[keys] = deepClone(source[keys])
		} else {
			targetObj[keys] = source[keys]
		}
	})
	return targetObj
}
/**
 * 当前时间转化
 * @param {String} str (y-m-d h:i:s) y:年 m:月 d:日 h:时 i:分 s:秒
 */
function getDateTime(str){
	let date = new Date(),
	year = date.getFullYear(), //年
	month = date.getMonth() + 1, //月
	day = date.getDate(), //日
	hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(), //时
	minute = date.getMinutes() < 10 ? date.getMinutes() : date.getMinutes(), //分
	second = date.getSeconds() < 10 ? date.getSeconds() : date.getSeconds(); //秒
	month >= 1 && month <= 9 ? (month = "0" + month) : "";
	day >= 0 && day <= 9 ? (day = "0" + day) : "";
	hour >= 0 && hour <= 9 ? hour : "";
	minute >= 0 && minute <= 9 ? (minute = "0" + minute) : "";
	second >= 0 && second <= 9 ? (second = "0" + second) : "";
	if(str.indexOf('y') != -1){
		str = str.replace('y', year)
	}
	if(str.indexOf('m') != -1){
		str = str.replace('m', month)
	}
	if(str.indexOf('d') != -1){
		str = str.replace('d', day)
	}
	if(str.indexOf('h') != -1){
		str = str.replace('h', hour)
	}
	if(str.indexOf('i') != -1){
		str = str.replace('i', minute)
	}
	if(str.indexOf('s') != -1){
		str = str.replace('s', second)
	}
	return str;
}

/**
 * 对象转url参数
 * @param {*} data,对象
 * @param {*} isPrefix,是否自动加上"?"
 */
function queryParams(data = {}, isPrefix = true, arrayFormat = 'brackets') {
	let prefix = isPrefix ? '?' : ''
	let _result = []
	if (['indices', 'brackets', 'repeat', 'comma'].indexOf(arrayFormat) == -1) arrayFormat = 'brackets';
	for (let key in data) {
			let value = data[key]
			// 去掉为空的参数
			if (['', undefined, null].indexOf(value) >= 0) {
					continue;
			}
			// 如果值为数组，另行处理
			if (value.constructor === Array) {
					// e.g. {ids: [1, 2, 3]}
					switch (arrayFormat) {
							case 'indices':
									// 结果: ids[0]=1&ids[1]=2&ids[2]=3
									for (let i = 0; i < value.length; i++) {
											_result.push(key + '[' + i + ']=' + value[i])
									}
									break;
							case 'brackets':
									// 结果: ids[]=1&ids[]=2&ids[]=3
									value.forEach(_value => {
											_result.push(key + '[]=' + _value)
									})
									break;
							case 'repeat':
									// 结果: ids=1&ids=2&ids=3
									value.forEach(_value => {
											_result.push(key + '=' + _value)
									})
									break;
							case 'comma':
									// 结果: ids=1,2,3
									let commaStr = "";
									value.forEach(_value => {
											commaStr += (commaStr ? "," : "") + _value;
									})
									_result.push(key + '=' + commaStr)
									break;
							default:
									value.forEach(_value => {
											_result.push(key + '[]=' + _value)
									})
					}
			} else {
					_result.push(key + '=' + value)
			}
	}
	return _result.length ? prefix + _result.join('&') : ''
}
 
function isTel (str) {
	let reg = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/
	return reg.test(str)
}

function saveImage () {
	if (wechat?.isWechat()) {
		wechat.callScanQRCode(() => {
			resolve(true)
		})
	} else {}
}

//车牌号验证方法

function isVehicleNumber(vehicleNumber) {
  // var xreg=/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
  var xxreg=/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DABCEFGHJK]$)|([DABCEFGHJK][A-HJ-NP-Z0-9][0-9]{4}$))/;  // 2021年新能源车牌不止有DF

  var creg=/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;

  if(vehicleNumber.length == 7){
    return creg.test(vehicleNumber);
  } else if(vehicleNumber.length == 8){
    return xxreg.test(vehicleNumber);
  } else{
    return false;
  }
}
module.exports = {
	getDateTime,
	queryParams,
	isTel,
	isVehicleNumber
}